import {createRouter, createWebHistory} from "vue-router";
import baseRoute from './config';

console.log('baseRoute',baseRoute);

const router = createRouter({
    history: createWebHistory(process.env.BASE_URL),
    routes:baseRoute
});

router.beforeEach((to, from, next) => {
    document.title = `${to.meta.title} | vue-manage-system`;
    const role = localStorage.getItem('ms_username');
    
    if (!role && to.path !== '/login') {
        next('/login');
    } else if (to.meta.permission) {
        // 如果是管理员权限则可进入，这里只是简单的模拟管理员权限而已
        role === 'admin'
            ? next()
            : next('/403');
    } else if(to.path == '/home'){
        // home页定向到第一个子节点
        next(to.matched[0]["children"][0]["path"]);
    }else{
        next();
    }
});

export default router;